package com.qd.qx.im;

import rx.Observable;
import rx.subjects.PublishSubject;
import rx.subjects.SerializedSubject;
import rx.subjects.Subject;

/*
 * 项目名:    SmackChat
 * 包名       com.azhong.smackchat.common.rxbus
 * 文件名:    RxBus
 * 创建者:    ZJB
 * 创建时间:  2017/3/6 on 11:43
 * 描述:     TODO
 */
public class ImRxBus {
    private static volatile ImRxBus mInstance;

    private final Subject bus;


    public ImRxBus() {
        bus = new SerializedSubject<>(PublishSubject.create());
    }

    /**
     * 单例模式RxBus
     *
     * @return
     */
    public static ImRxBus getInstance() {

        ImRxBus rxBus2 = mInstance;
        if (mInstance == null) {
            synchronized (ImRxBus.class) {
                rxBus2 = mInstance;
                if (mInstance == null) {
                    rxBus2 = new ImRxBus();
                    mInstance = rxBus2;
                }
            }
        }

        return rxBus2;
    }


    /**
     * 发送消息
     *
     * @param object
     */
    public void post(Object object) {

        bus.onNext(object);

    }

    /**
     * 接收消息
     *
     * @param eventType
     * @param <T>
     * @return
     */
    public <T> Observable<T> toObserverable(Class<T> eventType) {
        return bus.ofType(eventType);
    }
}
